import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created by jack on 18/4/1.
 */
public class LinkTree {
    private Queue<TreeLinkNode> queue = new LinkedList<>();
    public void connect(TreeLinkNode root){
        if(root == null){
            return ;
        }
        queue.add(root);
        con();
    }
    private void con(){
        int size = queue.size();
        if(size <= 0){
            return;
        }
        TreeLinkNode pre = null;
        for (int i = 0; i < size; i++) {
            if(i == 0){
                //代表了第一个
                pre = queue.poll();
            }else{
                TreeLinkNode n = queue.poll();
                pre.next = n;
                pre = n;
            }
            if(pre.left != null){
                queue.add(pre.left);
            }
            if(pre.right != null){
                queue.add(pre.right);
            }
        }
        pre.next = null;
        con();
    }
}
